#                                                   -*- shell-script -*-
#
# This file is part of the HDF4 build script. It is processed shortly
# after configure starts and defines, among other things, flags for
# the various compilation modes.

# Choosing C and Fortran Compilers
# --------------------------------------
#
# The user should be able to specify the compiler by setting the CC
# and F77 environment variables to the name of the compiler and any
# switches it requires for proper operation. If CC is unset then this
# script may set it. If CC is unset by time this script completes then
# configure will try `gcc' and `cc' in that order (perhaps some others
# too).
#
# Note: Code later in this file may depend on the value of $CC_BASENAME
#       in order to distinguish between different compilers when
#       deciding which compiler command-line switches to use.  This
#       variable is set based on the incoming value of $CC and is only
#       used within this file.

# The default compiler is `cc', which should map to clang
if test -z "$CC"; then
  CC=cc
  CC_BASENAME=cc
fi

# Figure out clang C compiler flags
. $srcdir/config/clang-flags

# Figure out GNU C compiler flags
. $srcdir/config/gnu-flags

# Figure out Intel oneAPI C compiler flags
. $srcdir/config/oneapi-flags

# Figure out Intel classic C compiler flags
. $srcdir/config/intel-flags

# The default compiler is 'f77', which should map to gfortran
if test "X-$F77" = "X-"; then
  F77=f77
  F77_BASENAME=f77
fi

# Figure out GNU FC compiler flags
. $srcdir/config/gnu-fflags

# compiler version strings

# check if the compiler_version_info is already set
if test -z "$cc_version_info"; then

case $CC in
    # whatever matches *pgcc* will also match *gcc*, so this one must come first
    *pgcc*)
        cc_version_info=`$CC $CFLAGS $H4_CFLAGS -V 2>&1 | grep 'pgcc'`
        ;;

    *gcc*)
        cc_version_info=`$CC $CFLAGS $H4_CFLAGS --version 2>&1 | grep -v 'PathScale' |\
            grep 'GCC' | sed 's/\(.*(GCC) [-a-z0-9\. ]*\).*/\1/'`
        ;;

    *icc*)
        cc_version_info=`$CC $CCFLAGS $H4_CCFLAGS -V 2>&1 | grep 'Version' |\
            sed 's/\(Intel.* Compiler\).*\( Version [a-z0-9\.]*\).*\( Build [0-9]*\)/\1\2\3/'`
        ;;

    *clang*)
        cc_version_info="`$CC $CFLAGS $H4_CFLAGS --version 2>&1 |\
            grep 'clang version' | sed 's/.*clang version \([-a-z0-9\.]*\).*/\1/'`"
        ;;

    *)
        echo "No match to get cc_version_info for $CC"
        ;;
esac

fi

# get fortran version info
case $F77 in
    *gfortran*)
        fc_version_info=`$F77 $FFLAGS --version 2>&1 |\
            grep 'GCC' | sed 's/\(.*(GCC) [-a-z0-9\. ]*\).*/\1/'`
        fc_version="`$F77 $FFLAGS -v 2>&1 |grep 'gcc version' |\
                sed 's/.*gcc version \([-a-z0-9\.]*\).*/\1/'`"
        if test X != "X$fc_version"; then
            fc_version=`echo $fc_version |sed 's/[-a-z]//g'`
        fi
        ;;

    *ifc*|*ifort*)
        fc_version_info=`$F77 $FFLAGS -V 2>&1 | grep 'Version' |\
            sed 's/\(Intel.* Compiler\).*\( Version [a-z0-9\.]*\).*\( Build [0-9]*\)/\1\2\3/'`
        fc_version="`$F77 $FFLAGS -V 2>&1 |grep '^Intel'`"
        if test X != "X$fc_version"; then
            fc_version="`echo $fc_version |sed 's/.*Version \([-a-z0-9\.\-]*\).*/\1/'`"
        fi
        ;;

    *f95*)
        # Figure out which compiler we are using: pgf90 or Absoft f95
        RM='rm -f'
        tmpfile=/tmp/cmpver.$$
        $F77 -V >$tmpfile
        if test -s "$tmpfile"; then
            if( grep -s 'Absoft' $tmpfile > /dev/null) then
                FC_BASENAME=f95
            fi
        fi
        $RM $tmpfile
        fc_version_info=`$F77 -V | grep Absoft`
        ;;

    *g95*|*g77*)
        fc_version_info=`$F77 $FFLAGS --version 2>&1 |\
            grep 'GCC'`
        ;;

    *pgf90*)
        fc_version_info=`$F77 $FFLAGS -V 2>&1 | grep 'pgf90'`
        fc_version="`$F77 $FFLAGS -V 2>&1 |grep '^pgf90 '`"
        if test X != "X$fc_version"; then
            fc_version=`echo $fc_version |sed 's/pgf90 \([-a-z0-9\.\-]*\).*/\1/'`
        fi
        ;;

     *)
        echo "No match to get fc_version_info for $F77"
        ;;
esac

if test X != "X$fc_version"; then
    # Get the compiler version numbers
    fc_vers_major=`echo $fc_version | cut -f1 -d.`
fi

# Overriding Configure Tests
# --------------------------
#
# Values for overriding configuration tests when cross compiling.

# Set this to `yes' or `no' depending on whether the target is big
# endian or little endian.
#ac_cv_c_bigendian=${ac_cv_c_bigendian='yes'}
